global.electron = require('electron');
window.ipcRenderer = require('electron').ipcRenderer;
window.remote = require('electron').remote;

const {
    contextBridge,
    ipcRenderer
} = require("electron");

//公开允许渲染器进程使用的受保护方法
//不公开整个对象的ipcRenderer

contextBridge.exposeInMainWorld(
    "api", {
        send: (channel, data) => {
            // 白名单频道
            let validChannels = ["toMain"];
            if (validChannels.includes(channel)) {
                ipcRenderer.send(channel, data);
            }
        },
        receive: (channel, func) => {
            let validChannels = ["fromMain"];
            if (validChannels.includes(channel)) {
                // 故意删除事件，因为它包含 “发件人”`
                ipcRenderer.on(channel, (event, ...args) => func(...args));
            }
        }
    }
);